//
//  AppDelegate.swift
//  placepic
//
//  Created by elesahich on 2020/06/29.
//  Copyright © 2020 elesahich. All rights reserved.
//

import UIKit

protocol UserIdentifyInterface {
    func checkUserToken()
    func setUserToken()
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    
    override init() {
        super.init()
        UIFont.overrideInitialize()
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        setUserToken()
        checkUserToken()
        return true
    }
}

extension AppDelegate: UserIdentifyInterface {
    
    func checkUserToken() {
        let flag = (UserDefaults.standard.string(forKey: "accessToken") != nil) ? true : false
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let storyboard = flag ? UIStoryboard(name: "Tabbar", bundle: nil) : UIStoryboard(name: "NewLogin", bundle: nil)
        let initialViewController = flag
            ? storyboard.instantiateViewController(withIdentifier: "TabbarController")
            : storyboard.instantiateViewController(withIdentifier: "SplashViewController")
        self.window?.rootViewController = initialViewController
        self.window?.makeKeyAndVisible()
    }

    func setUserToken() {
        // MARK: - Sprint2 릴리즈때 뺴자
        
        UserDefaults.standard.removeObject(forKey: "accessToken")
//        let tempToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZHgiOjg1LCJuYW1lIjoi7J207Iq57Zi4IiwiaWF0IjoxNTk4MDg4MDUxLCJleHAiOjE2MDA2ODAwNTEsImlzcyI6InBsYWNlcGljIn0.JFUQDERVUsOlO3YEVJIfIg5sRxZ1204EuPDydmgyYSQ"
//        UserDefaults.standard.set(tempToken, forKey: "accessToken")
    }
}

